//
// File: dwt.cpp
//
// MATLAB Coder version            : 3.4
// C/C++ source code generated on  : 16-Nov-2021 20:58:22
//

// Include Files
#include "rt_nonfinite.h"
#include "xiaoboband.h"
#include "dwt.h"
#include "xiaoboband_emxutil.h"
#include "convn_kernel.h"

// Function Declarations
static int div_s32(int numerator, int denominator);

// Function Definitions

//
// Arguments    : int numerator
//                int denominator
// Return Type  : int
//
static int div_s32(int numerator, int denominator)
{
  int quotient;
  unsigned int absNumerator;
  unsigned int absDenominator;
  boolean_T quotientNeedsNegation;
  if (denominator == 0) {
    if (numerator >= 0) {
      quotient = MAX_int32_T;
    } else {
      quotient = MIN_int32_T;
    }
  } else {
    if (numerator < 0) {
      absNumerator = ~(unsigned int)numerator + 1U;
    } else {
      absNumerator = (unsigned int)numerator;
    }

    if (denominator < 0) {
      absDenominator = ~(unsigned int)denominator + 1U;
    } else {
      absDenominator = (unsigned int)denominator;
    }

    quotientNeedsNegation = ((numerator < 0) != (denominator < 0));
    absNumerator /= absDenominator;
    if (quotientNeedsNegation) {
      quotient = -(int)absNumerator;
    } else {
      quotient = (int)absNumerator;
    }
  }

  return quotient;
}

//
// Arguments    : const double x_data[]
//                const int x_size[2]
//                double a_data[]
//                int a_size[2]
//                double d_data[]
//                int d_size[2]
// Return Type  : void
//
void dwt(const double x_data[], const int x_size[2], double a_data[], int
         a_size[2], double d_data[], int d_size[2])
{
  int nx;
  int k;
  int lx2;
  double y_data[146];
  int loop_ub;
  short J_data[146];
  double b_a_data[146];
  int b_a_size[1];
  double c_a_data[146];
  emxArray_real_T *r1;
  int ik;
  emxArray_real_T d_a_data;
  static const double B[10] = { 0.0033357252850015492, -0.012580751999015526,
    -0.0062414902130117052, 0.077571493840065148, -0.03224486958502952,
    -0.24229488706619015, 0.13842814590110342, 0.72430852843857441,
    0.60382926979747287, 0.160102397974125 };

  double z_data[137];
  emxArray_real_T e_a_data;
  static const double b_B[10] = { -0.160102397974125, 0.60382926979747287,
    -0.72430852843857441, 0.13842814590110342, 0.24229488706619015,
    -0.03224486958502952, -0.077571493840065148, -0.0062414902130117052,
    0.012580751999015526, 0.0033357252850015492 };

  if (x_size[1] == 0) {
    k = 18;
    memset(&y_data[0], 0, 18U * sizeof(double));
  } else {
    nx = x_size[1];
    lx2 = x_size[1] << 1;
    loop_ub = 18 + x_size[1];
    if (0 <= loop_ub - 1) {
      memset(&J_data[0], 0, (unsigned int)(loop_ub * (int)sizeof(short)));
    }

    if (x_size[1] < 9) {
      for (k = 1; k <= 9 - nx; k++) {
        ik = 10 - (k + lx2 * div_s32(9 - k, lx2));
        if (ik > nx) {
          ik = (lx2 - ik) + 1;
        }

        J_data[k - 1] = (short)ik;
      }

      for (k = 10 - x_size[1]; k < 10; k++) {
        J_data[k - 1] = (short)(10 - k);
      }
    } else {
      for (k = 0; k < 9; k++) {
        J_data[k] = (short)(9 - k);
      }
    }

    for (k = 1; k <= nx; k++) {
      J_data[8 + k] = (short)k;
    }

    if (x_size[1] < 9) {
      for (k = 1; k <= nx; k++) {
        J_data[(nx + k) + 8] = (short)((nx - k) + 1);
      }

      for (k = x_size[1] + 1; k < 10; k++) {
        ik = k - nx;
        if (ik > nx) {
          ik -= lx2 * div_s32(ik - 1, lx2);
          if (ik > nx) {
            ik = (lx2 - ik) + 1;
          }
        }

        J_data[(nx + k) + 8] = (short)ik;
      }
    } else {
      for (k = 0; k < 9; k++) {
        J_data[(nx + k) + 9] = (short)(nx - k);
      }
    }

    k = 18 + x_size[1];
    loop_ub = 18 + x_size[1];
    for (ik = 0; ik < loop_ub; ik++) {
      y_data[ik] = x_data[x_size[0] * (J_data[ik] - 1)];
    }
  }

  if (0 <= k - 1) {
    memcpy(&b_a_data[0], &y_data[0], (unsigned int)(k * (int)sizeof(double)));
  }

  b_a_size[0] = k;
  if (0 <= k - 1) {
    memcpy(&c_a_data[0], &b_a_data[0], (unsigned int)(k * (int)sizeof(double)));
  }

  emxInit_real_T1(&r1, 1);
  ik = r1->size[0];
  r1->size[0] = b_a_size[0] - 9;
  emxEnsureCapacity_real_T1(r1, ik);
  loop_ub = b_a_size[0];
  for (ik = 0; ik <= loop_ub - 10; ik++) {
    r1->data[ik] = 0.0;
  }

  d_a_data.data = (double *)&c_a_data;
  d_a_data.size = (int *)&b_a_size;
  d_a_data.allocatedSize = 146;
  d_a_data.numDimensions = 1;
  d_a_data.canFreeData = false;
  eml_conv2(r1, &d_a_data, B, 9, b_a_size[0] - 1);
  loop_ub = r1->size[0];
  for (ik = 0; ik < loop_ub; ik++) {
    z_data[ik] = r1->data[ik];
  }

  a_size[0] = 1;
  a_size[1] = ((x_size[1] + 7) >> 1) + 1;
  loop_ub = (x_size[1] + 7) >> 1;
  for (ik = 0; ik <= loop_ub; ik++) {
    a_data[ik] = z_data[1 + (ik << 1)];
  }

  if (0 <= k - 1) {
    memcpy(&b_a_data[0], &y_data[0], (unsigned int)(k * (int)sizeof(double)));
  }

  b_a_size[0] = k;
  if (0 <= k - 1) {
    memcpy(&c_a_data[0], &b_a_data[0], (unsigned int)(k * (int)sizeof(double)));
  }

  ik = r1->size[0];
  r1->size[0] = b_a_size[0] - 9;
  emxEnsureCapacity_real_T1(r1, ik);
  loop_ub = b_a_size[0];
  for (ik = 0; ik <= loop_ub - 10; ik++) {
    r1->data[ik] = 0.0;
  }

  e_a_data.data = (double *)&c_a_data;
  e_a_data.size = (int *)&b_a_size;
  e_a_data.allocatedSize = 146;
  e_a_data.numDimensions = 1;
  e_a_data.canFreeData = false;
  eml_conv2(r1, &e_a_data, b_B, 9, b_a_size[0] - 1);
  loop_ub = r1->size[0];
  for (ik = 0; ik < loop_ub; ik++) {
    z_data[ik] = r1->data[ik];
  }

  emxFree_real_T(&r1);
  d_size[0] = 1;
  d_size[1] = ((x_size[1] + 7) >> 1) + 1;
  loop_ub = (x_size[1] + 7) >> 1;
  for (ik = 0; ik <= loop_ub; ik++) {
    d_data[ik] = z_data[1 + (ik << 1)];
  }
}

//
// File trailer for dwt.cpp
//
// [EOF]
//
